package com.qf.md.unit.controller;

import com.qf.common.PageParam;
import com.qf.common.PageVO;
import com.qf.common.R;
import com.qf.entity.md.unit.MdUnitMeasure;
import com.qf.md.unit.param.UnitSearchParam;
import com.qf.md.unit.service.MdUnitMeasureService;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import lombok.extern.slf4j.Slf4j;
import org.springframework.web.bind.annotation.*;
import org.springframework.web.multipart.MultipartFile;

import javax.annotation.Resource;
import javax.servlet.http.HttpServletResponse;
import java.util.List;

@RestController
@RequestMapping("/md/unit")
@Api(tags = "计量单位管理")
@CrossOrigin

@Slf4j//就相当于 private final Logger log = LoggerFactory.getLogger(MdUnitMeasureController.class);
public class MdUnitMeasureController {
    @Resource
    private MdUnitMeasureService mdUnitMeasureService;

    /**
     * 导入接口
     * @param
     * @return
     */
    @PostMapping("/upload/excel")
    //MultipartFile 是 SpringMVC提供的一个接口，用于处理文件上传。
    public R uploadExcel(MultipartFile file) {
        //System.out.println(unitSearchParam);
        System.out.println(file);
        mdUnitMeasureService.uploadExcel(file);
        return R.ok("导入成功");
    }
    @GetMapping("/download/excel")
    public R downloadExcel(HttpServletResponse response) {
        mdUnitMeasureService.downloadExcel(response);
        return R.ok("下载成功");
    }

    @GetMapping("/list")
    //R<T> ok(T data)  T ===> PageVO<MdUnitMeasure> 把这个值赋值给了data
    @ApiOperation("分页查询")
    public R<PageVO<MdUnitMeasure>> unitPage(UnitSearchParam unitSearchParam) {
        log.info("info");
        log.warn("warn");
        log.error("error");
        log.debug("debug");
        PageVO<MdUnitMeasure> mdUnitMeasurePageVO = mdUnitMeasureService.unitPage(unitSearchParam);
        return R.ok(mdUnitMeasurePageVO);
        //{
        //  code:2000,
        //   msg:"",
        //   data:{
        //     total:100,
        //     dataList:[{},{},{}]
        //   }
        // }
    }
    @PostMapping("/save")
    public R<MdUnitMeasure> saveMdUnitMeasure(@RequestBody MdUnitMeasure mdUnitMeasure) {
        //.System.out.println(mdUnitMeasure);
        log.info("保存的数据为：{}",mdUnitMeasure);

        int i = mdUnitMeasureService.saveMdUnitMeasure(mdUnitMeasure);
        if (i > 0) {
            return R.ok(mdUnitMeasure);
        }
        return R.fail("保存失败");
    }
    @PostMapping("/update")
    public R<MdUnitMeasure> updateMdUnitMeasure(@RequestBody MdUnitMeasure mdUnitMeasure) {
        System.out.println(mdUnitMeasure);
        int i = mdUnitMeasureService.updateMdUnitMeasure(mdUnitMeasure);
        if (i > 0) {
            return R.ok(mdUnitMeasure);
        }
        return R.fail("保存失败");
    }
    @GetMapping("/info")
    public R<MdUnitMeasure> findById(@RequestParam Long id) {
        System.out.println(id);
        MdUnitMeasure mdUnitMeasure = mdUnitMeasureService.findById(id);
        return R.ok(mdUnitMeasure);
    }
    @GetMapping("/primaryUnit")
    public R<List<MdUnitMeasure>> findByPrimaryUnit() {
        List<MdUnitMeasure> byPrimaryUnit = mdUnitMeasureService.findByPrimaryUnit();
        return R.ok(byPrimaryUnit);
    }
    /**
     * 根据id删除数据
     */
    @PostMapping("/deletebyId")
    public R<String> delete(Long id) {
        System.out.println(id);
        mdUnitMeasureService.deleteMdUnitMeaeasure(id);
        return R.ok("删除成功");
    }
    @PostMapping("/deleteBatch")
    public R<String> deleteBatch(@RequestBody List<Long> ids) {
        System.out.println(ids);
        mdUnitMeasureService.deleteBatch(ids);
        return R.ok("删除成功");
    }

}
